﻿using System;
using System.Web;

using System.Data.SqlClient;
using System.Data;
using System.Web.SessionState;
using System.Text;

namespace IFD2
{
    public class city_suggestion : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (context.Request.QueryString["input"] == null)
            {
                context.Response.Write("?:Lost paramater!");
                context.ApplicationInstance.CompleteRequest();
                return;
            }
            //if (context.Session["USER"]==null || context.Session["USER"].ToString()=="")
            //{
            //    context.Response.Write("?:Unauthorized!");
            //    context.ApplicationInstance.CompleteRequest();
            //    return;
            //}
            SqlCommand cmd = new SqlCommand();
            SqlParameter para;
            StringBuilder buf = new StringBuilder();
            DataTable dt_ret = new DataTable();
            try
            {
                cmd.CommandText = "bll_city_get_input_suggestion";
                //like 版本：
                //para = cmd.Parameters.AddWithValue("@query_str", "%" + context.Request.QueryString["input"] + "%");
                //full-text search 版本：
                para = cmd.Parameters.AddWithValue("@query_str", context.Request.QueryString["input"]);
                TinyDAL.Exec(ref cmd, ref dt_ret);
                int j = dt_ret.Rows.Count;
                //foreach (DataRow row in dt_ret.Rows)
                for (int i = 0; i < j; i++)
                {
                    if (i + 1 < j)
                        buf.Append(dt_ret.Rows[i]["names"].ToString().TrimEnd() + "|");
                    else
                        buf.Append(dt_ret.Rows[i]["names"].ToString().TrimEnd());
                }
                if (j == 10) //SQL查询语句中只取前10个记录,可能还有记录未能显示。
                    buf.Append("|...:...");

                //Random rd = new Random();
                //buf.Append(rd.Next(1, 1000).ToString());

                context.Response.Write(buf.ToString());
                context.ApplicationInstance.CompleteRequest();
                return;
            }
            catch (Exception ex)
            {
                if (context.Application["DEBUG"].ToString() == "1")
                {
                    context.Response.Write(ex.Message);
                    context.ApplicationInstance.CompleteRequest();
                }
                else
                {
                    context.Response.Write("Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.");
                    context.ApplicationInstance.CompleteRequest();
                }
                return;
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}